package cn.wolfcode.trip.base.domain;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
/**
 * 商品表
 */
public class Product extends BaseDomain{
    //标题
    private String title;

    //封面
    private String coverUrl;

    //价格
    private BigDecimal price;

    //折扣
    private BigDecimal discount;

    //发布时间
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date releaseTime;

    //所属品牌
    private ProductBrand brand;

    //状态
    private Integer state;

    //详情内容
    private ProductContent productContent;

    public static final int STATE_AUDIT = 0;//默认
    public static final int STATE_RELEASE = 1;//热门
    public static final int STATE_REJECT = -1;//禁用

    //封装后台日报管理待审核网页需要的数据
    public String getStateName(){
        String stateName = "";
        switch (state){
            case 0:stateName = "默认"; break;
            case 1:stateName = "热门"; break;
            case -1:stateName = "禁用"; break;
        }
        return stateName;
    }

    //封装后台商城目录管理网页需要的数据
    public String getJson(){
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("title",title);
        map.put("coverUrl",coverUrl);
        map.put("price",price);
        map.put("discount",discount);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("releaseTime",simpleDateFormat.format(releaseTime));
        if (brand != null) {
            map.put("brandId", brand.getId());
            map.put("brandTitle", brand.getTitle());
            if (brand.getCatalog() != null) {
                map.put("catalogId", brand.getCatalog().getId());
            }
        }
        map.put("state",state);
        return JSON.toJSONString(map);
    }

}